<%@ page import="com.oval.util.JspHelper" %>
<%@ page import="java.util.* " %> 
<%@ page import="java.lang.* " %> 
<%@ page import="java.io.* " %>
<%@ page import="java.sql.* " %>
<%@ page import="com.oval.foundation.engine.*,com.oval.foundation.util.* " %> 
<%@ page import="com.oval.foundation.DefaultParams" %>
<%@ page contentType = "text/html;charset=UTF-8" %>
<% 
	String dbtype = DefaultParams.findParam("DBType");
	String serviceId = request.getParameter("serviceid");
	String p1check = request.getParameter("p1check"); 
	String p2check = request.getParameter("p2check"); 
	String p1value = request.getParameter("p1value"); 
	String p2value = request.getParameter("p2value"); 

	//获取SQL
	OvalSQL t_sql = new OvalSQL();
	HashMap h_sql = null;
	String sqlString = "";
	String serviceSql = "select servicesql from sysservicectrl where serviceid=" + serviceId;
	try{
		t_sql.open(serviceSql);
		h_sql = t_sql.getNextRow();
	}catch(Exception e){
		t_sql.close();
	}	
	Iterator sit = h_sql.values().iterator();
	while(sit.hasNext()){
		sqlString = (String)sit.next();
	}
	
	//替换SQL中参数
	if(!p1check.equals("0"))
		sqlString = sqlString.replace("v_parameter1",p1value);
	if(!p2check.equals("0"))
		sqlString = sqlString.replace("v_parameter2",p2value);

	
	//获取标题信息
	HashMap h_title = null;
	String strTitle = "";
	String sqlTitleData = "";
	if(serviceId != null && sqlString.toLowerCase().startsWith("select")){
		String sqlGetTitle = "select servicetitle from sysservicectrl where serviceid=" + serviceId;
		try{
			t_sql.open(sqlGetTitle);
			h_title = t_sql.getNextRow();
		}catch(Exception e){
			t_sql.close();
		}
	}
	if(h_title != null){
		Iterator tit = h_title.values().iterator();
		while(tit.hasNext()){
			strTitle = (String)tit.next();
			sqlTitleData = "select substr(column_value,1,instr(column_value,'(')-1) from table(get_string_tab('" + strTitle +"'))";
		}
	}
	
	if(t_sql != null)
		t_sql.close();	

	//拆分标题信息
	if(h_title != null && sqlString.toLowerCase().startsWith("select")){
		try{
			t_sql.open(sqlTitleData);
			h_title = t_sql.getNextRow();
		}catch(Exception e){
			t_sql.close();
		}
	}

	//执行SQL(SELECT)
	OvalSQL m_sql = new OvalSQL();
	Connection conn = null;
	Statement stmt=null; 
	ResultSet rs = null;
	String str_rs = null;
	boolean did = false;
	conn = m_sql.getConnection().getConnection();
	if(sqlString != null && sqlString.toLowerCase().startsWith("select")){
		if(conn != null){
			try{
				did = true;
				stmt = conn.createStatement(); 
				rs = stmt.executeQuery(sqlString); 
			}catch(SQLException e) { 
				e.printStackTrace();
			}
		}
	}
	
	//获取客户端IP地址
    String ip = request.getHeader("x-forwarded-for");
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("Proxy-Client-IP");
    }
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
        ip = request.getRemoteAddr();
    }
    
    //定义log插入sql
    String log_sql = "";
    String sqlString4log = sqlString.replace("'","''");
    if(dbtype.equals("oracle"))
		log_sql = "insert into sysservicelog values(?,0,?,?,sysdate)";
	if(dbtype.equals("db2"))
		log_sql = "insert into sysservicelog values(?,0,?,?,current timestamp)";
	if(dbtype.equals("sqlserver"))
		log_sql = "insert into sysservicelog values(?,0,?,?,getdate())";
	//记录LOG
	PreparedStatement ps = conn.prepareStatement(log_sql);
	ps.setInt(1,Integer.valueOf(serviceId).intValue());
	ps.setString(2,ip);
	ps.setString(3,sqlString);
	ps.executeUpdate();
	ps.close();

	
	//执行SQL(EXECUTE)
	if(sqlString != null && !sqlString.toLowerCase().startsWith("select")){
		did = true;
		str_rs = m_sql.execute(sqlString);
	}

	String contextPath = request.getContextPath();
%>
<link rel="stylesheet" href="<%=contextPath%>/console/css/chinese/csscontent.css" type="text/css"> 
<script language="javascript">
function page_back(){
	var serviceid = "<%=serviceId%>";
	var p1value = "<%=p1value%>";
	var p2value = "<%=p2value%>";
	var url = "DBManagerInfo.jsp?serviceid=" + serviceid;
	self.location.href = url;
}
</script>
<table width="100%" border="0" cellspacing="1" cellpadding="2" bgcolor="#999999">
<%
	Iterator it = null;
	int colscount = 0;
	//展示标题行
	if( h_title != null){
		out.println("<tr class=\"td_bg_row\">");
		while(h_title != null){
			it = h_title.values().iterator();
			while(it.hasNext()){
				out.println("<td class=\"td_title_bg\">"+it.next()+"</td>");
				colscount++;
			}
			h_title = t_sql.getNextRow();
		}
		out.println("</tr>");	
		if(t_sql != null)
			t_sql.close();
	}
	
	//返回EXECUTE语句执行结果
	if(str_rs != null){
		if(str_rs.toCharArray().length < 6 && Character.isDigit(str_rs.charAt(0))){  
			str_rs = ("Execute Sucessfully!"); 
		}
		out.println("<tr class=\"td_bg_row\">    <td>SQL :"+sqlString+"</td>  </tr>");
		out.println("<tr class=\"td_bg_row\">    <td>"+str_rs+"</td>  </tr>");
	}else if(rs.next()){
		//展示SELECT语句查询结果集
		out.println("<tr class=\"td_bg_row\">");
		for(int i=0;i<colscount;i++){
			Object t = rs.getString(i+1);
			if(t==null)
				out.println("<td class=\"td_center\"><div align=\"left\">"+t+"</td>");
			else
				out.println("<td class=\"td_center\"><div align=\"left\">"+t.toString()+"</td>");
		}  
		out.println("</tr>");
		while(rs.next()){
			out.println("<tr class=\"td_bg_row\">");
			for(int j=0;j<colscount;j++){
				Object t = rs.getString(j+1);
				if(t==null)
					out.println("<td class=\"td_center\"><div align=\"left\">"+t+"</td>");
				else
					out.println("<td class=\"td_center\"><div align=\"left\">"+t.toString()+"</td>");
			}  
			out.println("</tr>");
		}
		rs.close();
		stmt.close();
		conn.close();
	}else if(did){
		//SELECT已执行且无结果集
		out.println("<tr class=\"td_bg_row\">    <td colspan=" + colscount + ">SQL :"+sqlString+"</td>  </tr>");
		out.println("<tr class=\"td_bg_row\">    <td colspan=" + colscount + ">No record found. Please make sure that the table, column or the value exists.</td>  </tr>");
		rs.close();
		stmt.close();
		conn.close();
	}
	out.println("<tr class=\"td_bg_row\"><td colspan=" + colscount + "><div align=\"center\"><input type=\"button\" name=\" BACK \" value=\" BACK \" onclick=\"javascript:page_back()\"></div></td></tr>");
%>
</table>

